<?php
/**
*description:物流管理
*author:李飞@天众科技
*time:2016/1/6
*/
namespace Admin\Controller;
use Think\Controller;
class LogisticsController extends CommonController {
	//动态获取车次号
	public function ajax_batchno(){
		$t_no=serialNumber('LogisticsBatch');//流水号
		$order_no='CAR'.date('Ymd',time()).$t_no;//流水单号
		$return['status']=1;
		$return['order_no']=$order_no;
		$this->ajaxReturn($return);
	}
	public function car(){
		$m=M('Logistics',C('DB_PRS'),C('DB_DEPOT'));
		$logistics_list=$m->where(array('status'=>1))->select();
		$this->assign('logistics_list',$logistics_list);
		$this->display();
	}
	public function logistics(){
		$m=M('Logistics',C('DB_PRS'),C('DB_DEPOT'));
		$logistics_list=$m->where(array('status'=>1))->select();
		$this->assign('logistics_list',$logistics_list);
		$this->display();
	}
	//车次信息
	public function logistics_batch(){
		
		$this->display();
	}
	//车次绑定订单详情信息
	public function logisticsBatchInfo(){
		
		$this->display();
	}
	//当前车次订单列表
	public function get_logisticsOrder(){
		$m=M('LogisticsOrder',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        //搜索条件
        $map=array();
		// dump(I());
		$batch_id=I('batch_id');
		$order_no=I('order_no');
		$user_name=I('user_name');
		$user_phone=I('user_phone');
        // $batch_no=$ck->in('车次号','batch_no','ennumstr','',0,0);       
        !empty($order_no)?$map['p.order_no']=array('like','%'.$order_no.'%'):'';
		!empty($user_name)?$map['p.user_name']=array('like','%'.$user_name.'%'):'';
		!empty($user_phone)?$map['p.user_phone']=array('like','%'.$user_phone.'%'):'';
		$map['m.batch_id']=$batch_id;
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  
		
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
        
        $count=$m->alias('m')->join('__PUSHORDER__ p ON m.order_id= p.id')->where($map)->count();

        $info=$m->alias('m')->join('__PUSHORDER__ p ON m.order_id= p.id')->where($map)->page($page,$rows)->order('p.'.$sort.' '.$order)->select();
        
		// echo $m->getlastsql();
		// die;
		foreach($info as $k=>$v){
			$info[$k]['add_time']=date('Y-m-d',$v['add_time']);
		}
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
	}
	//当前车次列表
	public function get_logistics_batch(){
		$m=M('LogisticsBatch',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        //搜索条件
        $map=array();
		$car_id=I('car_id');
        $batch_no=$ck->in('车次号','batch_no','ennumstr','',0,0);       
        !empty($batch_no)?$map['batch_no']=array('like','%'.$batch_no.'%'):'';
		$map['car_id']=$car_id;
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  
		
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		$status=I('status');
		if(empty($status)){
			$map['status']=array('neq',-1);
		}else{
			$map['status']=$status;
		}
        
        $count=$m->where($map)->count();
        if(empty($map)){
            $info=$m->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
        }

		foreach($info as $k=>$v){
			$info[$k]['add_time']=date('Y-m-d',$v['add_time']);
			$info[$k]['send_time']=date('Y-m-d',$v['send_time']);
			$info[$k]['examine_time']=date('Y-m-d',$v['examine_time']);
			if(!$v['status']){
				$info[$k]['color']=0;
			}
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
	}
	//备案公司列表
	public function get_logistics(){
		$m=M('Logistics',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        //搜索条件
        $map=array();
        $logistics_company_no=$ck->in('订单编码','logistics_company_no','ennumstr','',0,0);       
        !empty($logistics_company_no)?$map['logistics_company_no']=array('like','%'.$logistics_company_no.'%'):'';
		$logistics_company_name=$ck->in('订单编码','logistics_company_name','ennumstr','',0,0);       
        !empty($logistics_company_name)?$map['logistics_company_name']=array('like','%'.$logistics_company_name.'%'):'';
	
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  
		
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		$status=I('status');
		if(empty($status)){
			$map['status']=array('neq',-1);
		}else{
			$map['status']=$status;
		}
        
        $count=$m->where($map)->count();
        if(empty($map)){
            $info=$m->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
        }

		foreach($info as $k=>$v){
			$info[$k]['add_time']=date('Y-m-d',$v['add_time']);
			if(!$v['status']){
				$info[$k]['color']=0;
			}
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
	}
	//订单列表
	public function get_car(){
		$m=M('LogisticsCar',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
        //搜索条件
        $map=array();
        $plate_number=$ck->in('订单编码','plate_number','ennumstr','',0,0);       
        !empty($plate_number)?$map['plate_number']=array('like','%'.$plate_number.'%'):'';
	
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);  
		
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
		$status=I('status');
		if(empty($status)){
			$map['status']=array('neq',-1);
		}else{
			$map['status']=$status;
		}
        
        $count=$m->where($map)->count();
        if(empty($map)){
            $info=$m->page($page,$rows)->order($sort.' '.$order)->select();
        }else{
            $info=$m->where($map)->page($page,$rows)->order($sort.' '.$order)->select();
        }

		foreach($info as $k=>$v){
			$info[$k]['add_time']=date('Y-m-d',$v['add_time']);
			if(!$v['status']){
				$info[$k]['color']=0;
			}
		}
		
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
	}
	//添加备案公司信息
    public function logisticsAdd(){
		$m=M('Logistics',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['logistics_company_name']=I('logistics_company_name');
		$data['logistics_company_no']=I('logistics_company_no');
		$data['weight']=I('weight');
		$data['weight_base_price']=I('weight_base_price');
		$data['weight_over_price']=I('weight_over_price');
		$data['volume']=I('volume');
		$data['volume_base']=I('volume_base');
		$data['add_time']=time();
		
        $result=$m->add($data);
        if(false===$result){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//添加车辆信息
    public function carAdd(){
		$m=M('LogisticsCar',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['plate_number']=I('plate_number');
		$data['car_volume']=I('car_volume');
		$data['vehicle_load']=I('vehicle_load');
		$data['car_cate']=I('car_cate');
		$data['car_brand']=I('car_brand');
		$data['logistics_company_no']=I('logistics_company_no');
		$data['add_time']=time();
		
        $result=$m->add($data);
        if(false===$result){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//添加车次信息
    public function logistics_batchAdd($car_id){
		$m=M('LogisticsBatch',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['car_id']=$car_id;
		$data['batch_no']=I('batch_no');
		$data['arrivel']=I('arrivel');
		$data['price']=I('price');
		$data['mileage']=I('mileage');
		$data['send_time']=strtotime(I('send_time'));
		$data['add_time']=time();
		
        $result=$m->add($data);
        if(false===$result){
            $return['message']='添加失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='添加成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//更新备案公司信息
    public function logisticsSave(){
		$m=M('Logistics',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['logistics_company_name']=I('logistics_company_name');
		$data['logistics_company_no']=I('logistics_company_no');
		$data['weight']=I('weight');
		$data['weight_base_price']=I('weight_base_price');
		$data['weight_over_price']=I('weight_over_price');
		$data['volume']=I('volume');
		$data['volume_base']=I('volume_base');
		
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	
	//更新车辆信息
    public function carSave(){
		$m=M('LogisticsCar',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		$data['plate_number']=I('plate_number');
		$data['car_volume']=I('car_volume');
		$data['vehicle_load']=I('vehicle_load');
		$data['car_cate']=I('car_cate');
		$data['car_brand']=I('car_brand');
		$data['logistics_company_no']=I('logistics_company_no');
		
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//更新车次信息
    public function logistics_batchSave(){
		$m=M('LogisticsBatch',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit; 
        $ck=A('CheckInput');
		// $data['batch_no']=I('batch_no');
		$data['arrivel']=I('arrivel');
		$data['price']=I('price');
		$data['mileage']=I('mileage');
		$data['send_time']=strtotime(I('send_time'));
		
        $map['id']=$ck->in('id','id','intval','',1,0);
		
        $result=$m->where($map)->save($data);
        if(false===$result){
            $return['message']='更新信息失败';
            $return['status']=false;
            $this->ajaxReturn($return);
        }else{
            $return['message']='更新信息成功!';
			$return['status']=true; 
			$this->ajaxReturn($return);
        }
    }
	//审核车次
    public function examinelogistics_batch(){
		//条件
		$mm=M('LogisticsBatch',C('DB_PRS'),C('DB_DEPOT'));
		$mmd=M('LogisticsOrder',C('DB_PRS'),C('DB_DEPOT'));
		$id=I('id');
		$value=I('value');
        if(empty($id)) exit;//为空
		$arr=explode(',', $id);
		
		foreach($arr as $keys=>$values){
			$order_info=$mm->where(array('id'=>$values))->find();
			$count=$mmd->where(array('batch_id'=>$values))->count();//当前车次订单数量
			// echo $mmd->getlastsql();
			if(!$count){
				$return['status']=0;
				$return['message']='车次号:'.$order_info['batch_no'].'，未添加订单不能审核！';
				$this->ajaxReturn($return);
			}
			$map['id']=$values;
			if($value==1){
				$data['status']=1;
			}else if($value==2){
				$data['status']=2;
			}else{
				$return['message']='审核状态参数错误!';
				$return['status']=false;
				$this->ajaxReturn($return);
				exit;
			}
			$data['examine_time']=time();
			
			$result=$mm->where($map)->save($data);
		}
		
		// echo $m->getlastsql();
		// die;
        if(!$result){
            $return['message']='审核失败!';
            $return['status']=false;  
        }else{
             $return['message']='审核成功!';
             $return['status']=true; 
        }        
       // return $return;
		// $return=tableExamine('ProductOrder');
        $this->ajaxReturn($return);
    }
	//发车
    public function sendcar(){
		//条件
		$so=M('StorageOutbound',C('DB_PRS'),C('DB_DEPOT'));
		$m=M('LogisticsBatch',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('LogisticsOrder',C('DB_PRS'),C('DB_DEPOT'));
		$mg=M('Pushordergoods',C('DB_PRS'),C('DB_DEPOT'));
		$ao=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		
		$id=I('id');
        if(empty($id)) exit;//为空
		$arr=explode(',', $id);

		foreach($arr as $keys=>$values){
			$order_list=$n->where(array('batch_id'=>$values))->select();
			foreach($order_list as $ks=>$vs){
				$goods_list=$mg->where(array('order_id'=>$vs['order_id']))->select();
				foreach($goods_list as $k=>$v){
						$map_ao['batch_no']=$v['goods_batch_no'];
						$ao_info=$ao->where($map_ao)->find();//批次详情
						$data['storage_amount']=array('exp','storage_amount-'.$v['goods_nums']);//剩余库存
						$res_ao=$ao->where($map_ao)->save($data);//批次减库存(根据批次号减库存)
						// $so->add(array('outbound_order_id'=>$info['id'],'inbound_order_id'=>$vs['goods_id'],'nums'=>$vs['nums'],'add_time'=>time(),'out_cate'=>3));//更新出库记录out_cate:(出库类型1:订单出库2:异常出库3:销售单出库数量4:调拨出库5:领料出库6:其它出库)
						if($res_ao===false){
							$return['message']='更新批次库存信息失败!';
							$return['status']=false;
							$this->ajaxReturn($return);
						}
						if($res_g===false){
							$return['message']='更新总库存信息失败!';
							$return['status']=false; 
							$this->ajaxReturn($return);
						}
				}
			}
			$m->where(array('id'=>$values))->setField('out_status',1);//更新发车状态
		}

		$return['message']='发车成功，更新库存信息成功!';
		$return['status']=true;
        $this->ajaxReturn($return);
    }
	//删除
    public function logistics_batchDel(){
		$return=tableDel('LogisticsBatch');
        $this->ajaxReturn($return);
    }
	//清空回收站
    public function logistics_batchClear(){
		$return=tableClear('LogisticsBatch');
        $this->ajaxReturn($return);
    }
	//恢复回收站数据
    public function logistics_batchUndo(){
		$return=tableUndo('LogisticsBatch');
        $this->ajaxReturn($return);
    }
	//审核
    public function examineLogistics(){
		$return=tableExamine('Logistics');
        $this->ajaxReturn($return);
    }
	//删除
    public function logisticsDel(){
		$return=tableDel('Logistics');
        $this->ajaxReturn($return);
    }
	//清空回收站
    public function logisticsClear(){
		$return=tableClear('Logistics');
        $this->ajaxReturn($return);
    }
	//恢复回收站数据
    public function logisticsUndo(){
		$return=tableUndo('Logistics');
        $this->ajaxReturn($return);
    }
	//审核
    public function examineCar(){
		$return=tableExamine('LogisticsCar');
        $this->ajaxReturn($return);
    }
	//删除
    public function carDel(){
		$return=tableDel('LogisticsCar');
        $this->ajaxReturn($return);
    }
	//清空回收站
    public function carClear(){
		$return=tableClear('LogisticsCar');
        $this->ajaxReturn($return);
    }
	//恢复回收站数据
    public function carUndo(){
		$return=tableUndo('LogisticsCar');
        $this->ajaxReturn($return);
    }
	//添加订单
    public function logisticsbatchorderadd(){
		$m=M('logisticsOrder',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('LogisticsBatch',C('DB_PRS'),C('DB_DEPOT'));
        if(!IS_POST) exit;
        $ck=A('CheckInput');  
        $id=$ck->in('id','id','string','',1,0);
		$batch_id=$ck->in('订单id','batch_id','intval','',1,0);
		//检查是否已审核（审核后不能再添加）
		$batch_info=$n->where(array('id'=>$batch_id))->find();
		if($batch_info['status']==1){
			$return['status']=0;
			$return['message']='已通过审核不能再添加！';
			$this->ajaxReturn($return);
		}
        if(empty($id)) exit;//为空
		$data_n['add_goods']=1;
		$data_m['batch_id']=$batch_id;
		// $data_m['goods_cate']=2;
		$arr=explode(',', $id);
		$i=0;//新增
		$j=0;//失败
		$k=0;//重复
		foreach($arr as $key=>$v){
			$data_m['order_id']=$v;
			$detail=$m->where($data_m)->find();
			// echo $m->getlastsql();
			if($detail){
				$k++;
			}else{
				$result=$m->add($data_m);
				// echo $m->getlastsql();
				// die;
				if($result){
					$i++;
				}else{
					$j++;
				}
			}
		}
		if($i){
			$n->where(array('id'=>$batch_id))->save($data_n);
			$message=$i.'个添加成功!';
		}
		if($j){
			$message=$j.'个添加失败!';
		}
		if($k){
			$message=$k.'个重复!';
		}
		$return['message']=$message;
        $return['status']=true;
        $this->ajaxReturn($return);
	}
	//订单列表
	public function get_order(){
        $m=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
        $ck=A('CheckInput');
		$outbound=A('Outbound');
        //搜索条件
        $map=array();
		// $map['pick_status']=0;//未生成波次单
		//拦截
		$inter_list=$outbound->zintercepted();//拦截订单列表
		$cc=count($inter_list);//拦截订单数量
		if($cc){ //cc大于0，则说明有拦截订单
			foreach($inter_list as $key=>$val){
				$arr[$key]=$val['order_no'];
			}
			$map['order_no']=array('not in',$arr); //移除拦截订单
		}
		$map['transport_cate']=3;
        $page=$ck->in('当前页','page','intval','1',0,0);   
        $rows=$ck->in('每页记录数','rows','intval','',0,0);
        //排序
        $sort=$ck->in('排序','sort','cnennumstr','id',0,0);
        $order=$ck->in('规则','order','cnennumstr','desc',0,0);
        
		$count=$m->alias('m')->join('__LOGISTICS_ORDER__ b ON m.id=b.order_id','LEFT')->where($map)->where('b.id is null')->count();
		$info=$m->alias('m')->field('m.*')->join('__LOGISTICS_ORDER__ b ON m.id=b.order_id','LEFT')->where($map)->where('b.id is null')->page($page,$rows)->order('m.'.$sort.' '.$order)->select();
	
		// echo $m->getlastsql();
		// die;
		$show_advance=I('show_advance');//显示隐藏信息
		foreach($info as $k=>$v){
			$s=M('Storage',C('DB_PRS'),C('DB_DEPOT'));
			$storage_info=$s->where(array('storage_code'=>$v['storage_code']))->find();
			$info[$k]['storage_code']=$storage_info['name'];
			
			if(!$show_advance){
				$info[$k]['user_name']=cut_str($v['user_name'],1,0).'**';
				$info[$k]['user_phone']=substr($v['user_phone'],0,7).'****';
				$info[$k]['user_idcard']=strlen($v['user_idcard'])==15?substr_replace($v['user_idcard'],"****",8,4):(strlen($v['user_idcard'])==18?substr_replace($v['user_idcard'],"****",10,4):"身份证位数错误！");
			}
			$info[$k]['add_time']=date("Y-m-d H:i:s",$v['add_time']);
			if($v['grounding_status']==0){
				$info[$k]['grounding_status']='否';
			}
			if($v['grounding_status']==1){
				$info[$k]['grounding_status']='是';
			}
		}
        if(!empty($info)){
            $data['total']=$count;
            $data['rows']=$info;
        }else{
            $data['total']=0;
            $data['rows']=0;
        }
		
        $this->ajaxReturn($data);
    }
	//删除订单
	public function delLogisticsBatchAdd(){
		$id=I('id');
		$batch_id=I('batch_id');
		if(empty($id)) exit;//为空
		$arr=explode(',',$id);
		$map['order_id']=array('in',$arr);//订单id
		$map['batch_id']=$batch_id;//车次id
		$m=M('LogisticsOrder',C('DB_PRS'),C('DB_DEPOT'));
		$res=$m->where($map)->delete();
		// echo $m->getlastsql();
		// die;
		if($res){
			$return['status']=true;
			$return['message']="删除成功！";
			$this->ajaxReturn($return);
		}else{
			$return['status']=false;
			$return['message']="删除失败！";
			$this->ajaxReturn($return);
		}
		
	}
	
}